package com.hzncc.scaffold.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Trainingprogram;
import com.hzncc.scaffold.entity.Vo.CommitTrainingProgramVo;
import com.hzncc.scaffold.mapper.TrainingprogramMapper;
import com.hzncc.scaffold.service.TrainingprogramService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class TrainingprogramServiceImpl extends ServiceImpl<TrainingprogramMapper, Trainingprogram> implements TrainingprogramService {

    @Autowired
    TrainingprogramService trainingprogramService;

    @Override
    public ResultT saveTrainingprogram(Trainingprogram trainingprogram) {
        boolean save = trainingprogramService.save(trainingprogram);
        if (save) {
            return ResultT.success("新增成功");
        } else {
            return ResultT.fail("新增失败");
        }
    }

    @Override
    public ResultT updateTrainingprogram(Trainingprogram trainingprogram) {
        boolean update = trainingprogramService.updateById(trainingprogram);
        if (update) {
            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }

    @Override
    public ResultT deleteTrainingprogram(Integer programId) {
        if (trainingprogramService.getById(programId) != null) {
            Trainingprogram trainingprogram = trainingprogramService.getById(programId);
            trainingprogram.setIsDelete(1);
            boolean update = trainingprogramService.updateById(trainingprogram);
            if (update) {
                return ResultT.success("删除成功");
            } else {
                return ResultT.fail("删除失败");
            }
        } else {
            return ResultT.fail("数据错误");
        }
    }

    @Override
    public ResultT stayAtTop(Integer programId, Integer stayAtTop) {
        if (stayAtTop != null && stayAtTop != 0 && stayAtTop != 1) {
            return ResultT.fail("数据错误");
        }
        if (trainingprogramService.getById(programId) != null) {
            Trainingprogram trainingprogram = trainingprogramService.getById(programId);
            trainingprogram.setStayAtTop(stayAtTop);
            boolean update = trainingprogramService.updateById(trainingprogram);
            if (update) {
                if (stayAtTop == 0) {
                    return ResultT.success("取消置顶成功");
                } else {
                    return ResultT.success("置顶成功");
                }
            } else {
                return ResultT.fail("置顶失败");
            }
        } else {
            return ResultT.fail("数据错误");
        }
    }

    @Override
    public ResultT search(Trainingprogram trainingprogram) {
        LambdaQueryWrapper<Trainingprogram> lqw = new LambdaQueryWrapper<>();
        lqw.eq(trainingprogram.getId() != null, Trainingprogram::getId, trainingprogram.getId());
        lqw.like(trainingprogram.getTitle() != null, Trainingprogram::getTitle, trainingprogram.getTitle());
        lqw.eq(trainingprogram.getCover() != null, Trainingprogram::getCover, trainingprogram.getCover());
        lqw.eq(trainingprogram.getEdPhase() != null, Trainingprogram::getEdPhase, trainingprogram.getEdPhase());
        lqw.eq(trainingprogram.getStatus() != null, Trainingprogram::getStatus, trainingprogram.getStatus());
        lqw.eq(trainingprogram.getMajorId() != null, Trainingprogram::getMajorId, trainingprogram.getMajorId());
        lqw.eq(trainingprogram.getPubTime() != null, Trainingprogram::getPubTime, trainingprogram.getPubTime());
        lqw.eq(trainingprogram.getContent() != null, Trainingprogram::getContent, trainingprogram.getContent());
        lqw.eq(trainingprogram.getStayAtTop() != null, Trainingprogram::getStayAtTop, trainingprogram.getStayAtTop());
        lqw.eq(trainingprogram.getSortTime() != null, Trainingprogram::getSortTime, trainingprogram.getSortTime());
        lqw.eq(trainingprogram.getIsDelete() != null, Trainingprogram::getIsDelete, trainingprogram.getIsDelete());
        lqw.eq(trainingprogram.getPublisherId() != null, Trainingprogram::getPublisherId, trainingprogram.getPublisherId());

        List<Trainingprogram> trainingprogramList = trainingprogramService.list(lqw);
        return ResultT.success(trainingprogramList);
    }

    @Override
    public ResultT commit(CommitTrainingProgramVo commitTrainingProgramVo) {
        Trainingprogram trainingprogram = BeanUtil.toBean(commitTrainingProgramVo, Trainingprogram.class);
        trainingprogram.setSortTime(DateUtil.now());
        boolean save = trainingprogramService.save(trainingprogram);
        if(save){
            return ResultT.success(trainingprogram.getId());
        }else{
            return ResultT.fail("新增失败");
        }
    }
}
